package com.mars.module.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mars.common.response.PageInfo;
import com.mars.framework.context.ContextUserInfoThreadHolder;
import com.mars.module.admin.service.ISysDictDataService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import com.mars.module.admin.request.SysDrugRequest;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import com.mars.module.admin.mapper.SysDrugMapper;
import org.springframework.beans.BeanUtils;
import com.mars.module.admin.entity.SysDrug;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.mars.module.admin.service.ISysDrugService;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 药品业务层处理
 *
 * @author mars
 * @date 2024-05-15
 */
@Slf4j
@Service
@AllArgsConstructor
public class SysDrugServiceImpl implements ISysDrugService {

    private final SysDrugMapper baseMapper;
    private final ISysDictDataService iSysDictDataService;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public SysDrug add(SysDrugRequest request) {
        SysDrug entity = SysDrug.builder().build();
        BeanUtils.copyProperties(request, entity);
        Long id = ContextUserInfoThreadHolder.get().getId();
        entity.setCreateById(id);
        baseMapper.insert(entity);
        return entity;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean delete(Long id) {
        return baseMapper.deleteById(id) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteBatch(List<Long> ids) {
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean update(SysDrugRequest request) {
        SysDrug entity = SysDrug.builder().build();
        BeanUtils.copyProperties(request, entity);
        return baseMapper.updateById(entity) > 0;
    }

    @Override
    public SysDrug getById(Long id) {
        SysDrug drug = baseMapper.selectById(id);
        drug.setIsPrescriptionName(iSysDictDataService.getDictDataById("is_prescription", drug.getIsPrescription().toString()));
        drug.setDrugTypeName(iSysDictDataService.getDictDataById("drug_type", drug.getDrugType().toString()));
        return drug;
    }

    @Override
    public PageInfo<SysDrug> pageList(SysDrugRequest request) {
        Page<SysDrug> page = new Page<>(request.getPageNo(), request.getPageSize());
        LambdaQueryWrapper<SysDrug> query = this.buildWrapper(request);
        IPage<SysDrug> pageRecord = baseMapper.selectPage(page, query);
        List<SysDrug> records = pageRecord.getRecords();
        List<SysDrug> recordsNew = new ArrayList<>();
        for (SysDrug drug : records) {
            drug.setIsPrescriptionName(iSysDictDataService.getDictDataById("is_prescription", drug.getIsPrescription().toString()));
            drug.setDrugTypeName(iSysDictDataService.getDictDataById("drug_type", drug.getDrugType().toString()));
            recordsNew.add(drug);
        }
        pageRecord.setRecords(recordsNew);
        return PageInfo.build(pageRecord);
    }


    @Override
    public List<SysDrug> list(SysDrugRequest request) {
        LambdaQueryWrapper<SysDrug> query = this.buildWrapper(request);
        return baseMapper.selectList(query);
    }

    private LambdaQueryWrapper<SysDrug> buildWrapper(SysDrugRequest param) {
        LambdaQueryWrapper<SysDrug> query = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(param.getName())) {
            query.like(SysDrug::getName, param.getName());
        }
        if (param.getDrugType() != null) {
            query.like(SysDrug::getDrugType, param.getDrugType());
        }
        if (param.getIsPrescription() != null) {
            query.like(SysDrug::getIsPrescription, param.getIsPrescription());
        }
        if (StringUtils.isNotBlank(param.getDrugUsage())) {
            query.like(SysDrug::getDrugUsage, param.getDrugUsage());
        }
        if (StringUtils.isNotBlank(param.getMainTreat())) {
            query.like(SysDrug::getMainTreat, param.getMainTreat());
        }
        if (StringUtils.isNotBlank(param.getLocation())) {
            query.like(SysDrug::getLocation, param.getLocation());
        }
        return query;
    }

}
